<template>
  <div class="singer">
     <listview @select="selectSinger" :data="singerList"></listview>
      <router-view></router-view>
  </div>
</template>

<script type="text/ecmascript-6">
  import { getSingerList } from '../../api/singer'
  import { ERR_OK } from '../../api/config'
  import Singer from '../../common/js/singer'
  import Listview from '../../base/listview/listview'
  import { mapMutations } from 'vuex'

  const HOT_NAME = '热门'
  const HOT_SINGER_LEN = 10

  export default {
    data () {
      return {
        singerList: []
      }
    },
    created () {
      this._getSingerList()
    },
    methods: {
      _getSingerList () {
        getSingerList((res) => {
          if (ERR_OK === res.data.code) {
            this.singerList = res.data.singerList.data.singerlist
           // const kindex = res.data.singerList.data.index
         //   this._normalizeSiger(this.singerList, kindex)
          }
        })
      },
      selectSinger (singer) {
        this.$router.push({
          path: `/singer/${singer.singer_mid}`
        })
        this.setSinger(singer)
      },
      _normalizeSiger (list, kindex) {
        let map = {
          hot: {
            title: HOT_NAME,
            items: []
          }
        }
        list.forEach((item, index) => {
          if (index < HOT_SINGER_LEN) {
            map.hot.items.push(new Singer({
              id: item.singer_id,
              name: item.singer_name,
              mid_id: item.singer_mid
            }))
          }

//          const key = kindex
//          console.log(key)
//          if (!map[key]) {
//            map[key] = {
//              title: key,
//              items: []
//            }
//          }
//          map[key].items.push(new Singer({
//            id: item.singer_id,
//            name: item.singer_name,
//            mid_id: item.singer_mid
//          }))
//          console.log(map)
        })
//        let ret = []
//        let hot = []
//        for (let key in map) {
//          let val = map[key]
//          if (val.title.match(/[a-zA-Z]/)) {
//            ret.push(val)
//          } else if (val.title === HOT_NAME) {
//            hot.push(val)
//          }
//        }
//        ret.sort((a, b) => {
//          return a.title.charCodeAt(0) - b.title.charCodeAt(0)
//        })
//        return hot.concat(ret)
      },
      ...mapMutations({
        setSinger: 'SET_SINGER'
      })
    },
    components: {
      Listview
    }
  }
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
  .singer
    position: fixed
    top: 88px
    color:#999
    bottom: 0
    width: 100%
</style>
